package com.aistar.service.impl;

import com.aistar.mapper.ProgramMapper;
import com.aistar.pojo.Program;
import com.aistar.pojo.ProgramExample;
import com.aistar.service.ProgramService;
import com.aistar.util.MathUtil;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProgramServiceImpl implements ProgramService {

    private final ProgramMapper mapper;

    @Autowired
    public ProgramServiceImpl(ProgramMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public ServerResponse add(Program program) {
        int rows = mapper.insert(program);
        if (rows > 0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse delete(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        Program program = mapper.selectByPrimaryKey(id);
        if (program != null)
            return ServerResponse.getSuccess(program);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse modify(Program program) {
        int rows = mapper.updateByPrimaryKeySelective(program);
        if (rows > 0)
            return ServerResponse.modifySuccess();
        return ServerResponse.modifyFailed();
    }

    @Override
    public ServerResponse getByPage(Integer pageNum, Integer pageSize) {
        Page<Program> page = PageHelper.startPage(pageNum, pageSize);
        ProgramExample example = new ProgramExample();
        example.createCriteria().andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("program_id desc");
        List<Program> programList = mapper.selectByExample(example);
        if (programList != null && programList.size() > 0)
            return ServerResponse.getSuccess(page.toPageInfo());
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getHot(Integer pageSize) {
        PageHelper.startPage(1, 3 * pageSize);
        ProgramExample example = new ProgramExample();
        example.createCriteria().andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("program_difficult_degree desc");
        List<Program> programList = mapper.selectByExample(example);
        if (programList != null && programList.size() > 0) {
            int[] seeds = MathUtil.getRandomArray(pageSize, 0, programList.size());
            List<Program> output = new ArrayList<>();
            for (int seed : seeds) {
                output.add(programList.get(seed));
            }
            return ServerResponse.getSuccess(output);
        }
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getAll() {
        // ProgramExample example = new ProgramExample();
        List<Program> programs = mapper.selectByExample(null);
        if (programs != null && programs.size() > 0)
            return ServerResponse.getSuccess(programs);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByKeywords(Integer pageNum, Integer pageSize, String keywords) {
        Page<Program> page = PageHelper.startPage(pageNum, pageSize);
        ProgramExample example = new ProgramExample();
        ProgramExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        if (keywords != null)
            criteria.andProgramLanguageEqualTo(keywords);
        example.setOrderByClause("program_id desc");
        List<Program> programList = mapper.selectByExample(example);
        if (programList != null && programList.size() > 0)
            return ServerResponse.getSuccess(page.toPageInfo());
        return ServerResponse.getFailed();
    }
}
